import { Injectable } from "@nestjs/common";
import { Repository } from "typeorm";
import { Student } from "./student.entity";
import { InjectRepository } from "@nestjs/typeorm";

@Injectable()
export class StudentsService {
    //构造函数注入Studnet实体的Repository
    constructor(
        @InjectRepository(Student)
        private studentRepository: Repository<Student>//私有属性 存储仓库实例
    ) { }

    //查询所有学生
    findAll(): Promise<Student[]> {
        //调用Repository的find 方法 返回所有记录
        return this.studentRepository.find();
    }
    //根据ID查询单个学生
    findOne(id: number): Promise<Student | null> {
        return this.studentRepository.findOneBy({ id });
    }
    //创建学生
    async create(student: Student): Promise<Student> {
        //save 方法插入新纪录自动处理主键
        return this.studentRepository.save(student);
    }
    //更新，删除
    async update(id: number, student: Student): Promise<void> {
        await this.studentRepository.update(id, student);
    }
    async delete(student: Student): Promise<Student> {
        //save 方法插入新纪录自动处理主键
        return this.studentRepository.save(student);
    }
    // async update(id:number,student:Student):Promise<void>{
    //     await this.studentRepository.update(id:student);
    // }

}